﻿using SvgApplication.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace SvgApplication.Controllers
{
    public abstract class AuthorizationControllerBase : Controller
    {
        public virtual UserData CurrentUser { get; set; }

        public AuthorizationControllerBase()
        {
        }

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
            LoginUser(requestContext);
        }

        /// <summary>
        /// logs the user in the system, extracting data from the passed cookie and setting the HttpContext.User property
        /// </summary>
        /// <param name="requestContext"></param>
        private void LoginUser(System.Web.Routing.RequestContext requestContext)
        {
            try
            {
                string cookie = FormsAuthentication.FormsCookieName;
                HttpCookie httpCookie = requestContext.HttpContext.Request.Cookies[cookie];

                if (httpCookie == null) return;
                FormsAuthenticationTicket ticket = null;
                try
                {
                    ticket = FormsAuthentication.Decrypt(httpCookie.Value);
                }
                catch
                {
                }
                if (ticket == null || ticket.Expired) return;

                FormsIdentity identity = new FormsIdentity(ticket);
                UserData udata = UserData.CreateUserData(ticket.UserData);
                AuthenticationPrincipal principal = new AuthenticationPrincipal(identity, udata);
                requestContext.HttpContext.User = principal;

                //set the current user property, so we can access the current user instance easier 
                CurrentUser = udata;
            }
            catch (Exception ex)
            {
                //Logger.Log.ErrorToDb(ex);
            }
        }

        protected void LoginUser(int id, string name, string userToken)
        {
            AuthenticationHelper authHelper = new AuthenticationHelper();
            UserData ud = new UserData(id, name);
            ud.UserToken = userToken;
            authHelper.DoLogin(HttpContext, ud,10);

        }

        /// <summary>
        /// returns the current user authorization token, if it exists
        /// </summary>
        /// <returns></returns>
        protected string GetUserToken()
        {
            string userToken = string.Empty;
            if (CurrentUser != null)
            {
                userToken = CurrentUser.UserToken;
            }
            return userToken;
        }



        #region IDisposable

        protected override void Dispose(bool disposing)
        {
            DisposeController();
            base.Dispose(disposing);
        }

        private void DisposeController()
        {

        }
        #endregion

    }
}